package com.xuecheng.media.controller;

import com.xuecheng.api.media.MediaApi;
import com.xuecheng.api.media.dto.MediaDTO;
import com.xuecheng.api.media.qo.QueryMediaModel;
import com.xuecheng.api.media.vo.MediaVO;
import com.xuecheng.content.common.domain.page.PageRequestParams;
import com.xuecheng.content.common.domain.page.PageVO;
import com.xuecheng.content.common.domain.response.RestResponse;
import com.xuecheng.content.common.util.SecurityUtil;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping
public class MediaController implements MediaApi {

    public static final Long OPERATION_FLAG = -99887799L;

    @Autowired
    private MediaService mediaService;

    @Override
    @PostMapping("media")
    public MediaDTO createMedia(@RequestBody MediaVO vo) {

        //1.获得机构的id值
        Long companyId = SecurityUtil.getCompanyId();

        //2.将vo数据转换为dto数据
        MediaDTO dto = MediaConvert.INSTANCE.vo2dto(vo);
        dto.setCompanyId(companyId);

        //3.调用service获得结果数据
        MediaDTO result = mediaService.createMedia(dto);

        return result;
    }
    /*
    查询单个培训机构的id
     */

    @PostMapping("media/list")
    public PageVO queryMediaList(PageRequestParams params,@RequestBody QueryMediaModel model) {

        Long companyId = SecurityUtil.getCompanyId();

        PageVO pageVO = mediaService.queryMediaList(params,model,companyId);

        return pageVO;

    }

    /*
    删除媒资信息
     */
    @Override
    @DeleteMapping("media/{id}")
    public Boolean removeMediaId(@PathVariable long id) {

        Long companyId = SecurityUtil.getCompanyId();

        Boolean aBoolean = mediaService.removeMediaId(id, companyId);
        return aBoolean;
    }
    /*
    媒资信息审核接口定义-运营
     */
    @Override
    @PutMapping("m/media/audit")
    public boolean auditMedia(@RequestBody MediaVO vo) {

        boolean b = mediaService.auditMedia(vo);
        return b;
    }


    /*
    查询所有--运营
     */
    @PostMapping("m/media/list")
    public PageVO queryMediaMList(PageRequestParams params, QueryMediaModel model) {
        //调用service层课程查询的的方法，这里和查询每个机构的课程用的一个方法，
        PageVO pageVO = mediaService.queryMediaList(params, model, OPERATION_FLAG);

        return pageVO;
    }

    /*
    获得媒资信息预览资源地址 -----这个是机构预览视频
    浏览视频
     */
    @Override
    @GetMapping("/media/preview/{mediaId}")
    public String previewMedia(@PathVariable Long mediaId) {
        Long companyId = SecurityUtil.getCompanyId();
        String url = mediaService.previewMedia(mediaId, companyId);

        return url;
    }

     /*
    获得媒资信息预览资源地址 -----这个是运营预览视频
    浏览视频
     */
    @Override
    @GetMapping("/m/media/preview/{mediaId}")
    public String previewMMedia(@PathVariable Long mediaId) {

        String url = mediaService.previewMedia(mediaId,OPERATION_FLAG);

        return url;

    }

    @Override
    @GetMapping("/l/media/{mediaId}")
    public RestResponse getMediaById(@PathVariable Long mediaId) {

        RestResponse<MediaDTO> response = mediaService.getById4Service(mediaId);

        return response;

    }


}